var submitted = false;
var fail_gif = "img/commons/fail_min.gif";
var fail_font_color = "red";
var success_gif = "img/commons/success_min.gif";
function empity(Msg, Img, Font, FieldName) {
	Msg.style.display = "block";
	Img.src = fail_gif;
	Font.color = fail_font_color;
	Font.size = 3;
	Font.innerHTML = FieldName + "不能为空";
}
function tooLong(Msg, Img, Font, FieldName, MaxLength) {
	Msg.style.display = "block";
	Img.src = fail_gif;
	Font.color = fail_font_color;
	Font.size = 3;
	Font.innerHTML = FieldName + "超过最大长度" + MaxLength;
}
function tooShort(Msg, Img, Font, FieldName, MinLength) {
	Msg.style.display = "block";
	Img.src = fail_gif;
	Font.color = fail_font_color;
	Font.size = 3;
	Font.innerHTML = FieldName + "小于最小长度" + MinLength;
}

//function CheckUsername(FieldID, FieldName, MaxLength, MinLength) {
//	var Field = document.getElementById(FieldID);
//	var Msg = document.getElementById(FieldID + "_div");
//	var Img = document.getElementById(FieldID + "_img");
//	var Font = document.getElementById(FieldID + "_font");
//	if (Field.value == "") {
//		empity(Msg, Img, Font, FieldName);
//		return false;
//	} else if (Field.value.length > MaxLength) {
//		tooLong(Msg, Img, Font, FieldName, MaxLength);
//		return false;
//	} else if (Field.value.length < MinLength) {
//		tooShort(Msg, Img, Font, FieldName, MinLength);
//		return false;
//	} else {
//		if (submitted == false) {
//			$.ajax( {
//				type : "POST",
//				url : 'verifyUser/' + Field.value + '.html',
//				data : null,
//				success : function(msg) {
//					if (msg == "false") {
//						Msg.style.display = "block";
//						Img.src = success_gif;
//						Font.innerHTML = "";
//					} else {
//						Msg.style.display = "block";
//						Img.src = fail_gif;
//						Font.color = fail_font_color;
//						Font.innerHTML = "用户名已经被注册";
//					}
//				}
//			});
//		}
//		if(Font.innerHTML == "") return true;
//		else return false;
//	}
//}

function Focus(FieldID, FieldName, MaxLength, MinLength) {
	var Field = document.getElementById(FieldID);
	var Msg = document.getElementById(FieldID + "_div");
	var Img = document.getElementById(FieldID + "_img");
	var Font = document.getElementById(FieldID + "_font");
	Msg.style.display = "block";
	Img.src = "img/commons/caution_min.gif";
	Font.color = "#0d4659";
	Font.size = 3;
	Font.innerHTML = FieldName + "长度为:" + MinLength + "-" + MaxLength + "";

}
function CheckPassword(FieldID, FieldName, MaxLength, MinLength) {
	var Field = document.getElementById(FieldID);
	var Msg = document.getElementById(FieldID + "_div");
	var Img = document.getElementById(FieldID + "_img");
	var Font = document.getElementById(FieldID + "_font");
	if (Field.value == "") {
		empity(Msg, Img, Font, FieldName);
		return false;
	} else if (Field.value.length > MaxLength) {
		tooLong(Msg, Img, Font, FieldName, MaxLength);
		return false;
	} else if (Field.value.length < MinLength) {
		tooShort(Msg, Img, Font, FieldName, MinLength);
		return false;
	} else {
		Msg.style.display = "block";
		Img.src = success_gif;
		Font.innerHTML = "";
		return true;
	}
}
function CheckPasswordConfirm(PasswordFiled, FieldName, FieldID) {
	var Field = document.getElementById(FieldID);
	var Password = document.getElementById(PasswordFiled);
	var Msg = document.getElementById(FieldID + "_div");
	var Img = document.getElementById(FieldID + "_img");
	var Font = document.getElementById(FieldID + "_font");
	if (Field.value == "") {
		empity(Msg, Img, Font, FieldName);
		return false;
	} else if (Field.value != Password.value) {
		Msg.style.display = "block";
		Img.src = fail_gif;
		Font.color = fail_font_color;
		Font.innerHTML = "两次输入不一致";
		return false;
	} else {
		Msg.style.display = "block";
		Img.src = success_gif;
		Font.innerHTML = "";
		return true;
	}
}
function CheckEmail() {
	var Email = document.getElementById("email");
	var Msg = document.getElementById("email_div");
	var Img = document.getElementById("email_img");
	var Font = document.getElementById("email_font");
	var pattern = /^([a-zA-Z0-9._-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
	if (Email.value == "") {
		empity(Msg, Img, Font, 'E-amil');
		return false;
	} else if (!pattern.test(Email.value)) {
		Msg.style.display = "block";
		Img.src = fail_gif;
		Font.color = fail_font_color;
		Font.innerHTML = "E-mail格式不正确";
		return false;
	} else {
//		Msg.style.display = "block";
//		Img.src = success_gif;
//		Font.innerHTML = "";
//		return true;
		$.ajax( {
			type : "POST",
			url : 'verifyuser/' + Email.value + '.html',
			data : null,
			success : function(msg) {
			//alert(msg);
				if (msg == "false") {
					Msg.style.display = "block";
					Img.src = success_gif;
					Font.innerHTML = "";
					return true;
				} else {
					Msg.style.display = "block";
					Img.src = fail_gif;
					Font.color = fail_font_color;
					Font.innerHTML = "该E-Mail已经被注册";
					return false;
				}
			}
		});
	}
}
function Regist() {
	submitted = true;
	if (CheckPassword('password', '密码', 32, 6) == false)
		return false;
	if (CheckPasswordConfirm('password', '密码', 'passwordConfirm') == false)
		return false;
	if (CheckEmail() == false)
		return false;
	if (CheckCertCode('certcode', '验证码') == false) {
		submitted = false;
		return false;
	}
	document.getElementById("regist").submit();

}
function CheckCertCode(FieldID, FieldName) {
	var Field = document.getElementById(FieldID);
	var Msg = document.getElementById(FieldID + "_div");
	var Img = document.getElementById(FieldID + "_img");
	var Font = document.getElementById(FieldID + "_font");
	if (Field.value == "") {
		empity(Msg, Img, Font, FieldName);
		return false;
	} else {
		if (submitted == false) {
			$.ajax( {
				type : "POST",
				url : 'certcode/verify/' + Field.value + '.html',
				data : null,
				success : function(msg) {
					if (msg == "true") {
						Msg.style.display = "block";
						Img.src = success_gif;
						Font.innerHTML = "";
					} else {
						Msg.style.display = "block";
						Img.src = fail_gif;
						Font.color = fail_font_color;
						Font.innerHTML = "验证码错误";
					}
				}
			});
		}
		if(Font.innerHTML == "") return true;
		else return false;
	}
}
function ChangCertPic() {
	var certpic = document.getElementById('certpic');
	certpic.src = 'util/MakeCertPic.jpg?a=' + Math.random();
}
